// local.cpp
#define _WIN32_DCOM
#include <iostream.h>
#include <stdio.h>
#include "Component\component.h"
#include "registry.h"

HANDLE g_hEvent;

class CInsideCOM : public ISum
{
public:
	// IUnknown
	ULONG __stdcall AddRef();
	ULONG __stdcall Release();
	HRESULT __stdcall QueryInterface(REFIID iid, void** ppv);

	// ISum
	HRESULT __stdcall Sum(int x, int y, int *retval);
	HRESULT __stdcall Sum2(myDataType test, int *retval);

	CInsideCOM();
	~CInsideCOM();

private:
	long m_cRef;
};

CInsideCOM::CInsideCOM() : m_cRef(1)
{
	CoAddRefServerProcess();
}

CInsideCOM::~CInsideCOM()
{
	cout << "Component: CInsideCOM::~CInsideCOM()" << endl;
	if(CoReleaseServerProcess() == 0)
		SetEvent(g_hEvent);
}

ULONG CInsideCOM::AddRef()
{
	cout << "Component: CInsideCOM::AddRef() m_cRef = " << m_cRef + 1 << endl;
	return InterlockedIncrement(&m_cRef);
}

ULONG CInsideCOM::Release()
{
	cout << "Component: CInsideCOM::Release() m_cRef = " << m_cRef - 1 << endl;
	unsigned cRef = InterlockedDecrement(&m_cRef);
	if(cRef != 0)
		return cRef;
	delete this;
	return 0;
}

HRESULT CInsideCOM::QueryInterface(REFIID iid, void **ppv)
{
	if(iid == IID_IUnknown)
	{
		cout << "Component: CInsideCOM::QueryInterface() for IUnknown" << endl;
		*ppv = (IUnknown*)this;
	}
	else if(iid == IID_ISum)
	{
		cout << "Component: CInsideCOM::QueryInterface() for ISum" << endl;
		*ppv = (ISum*)this;
	}
	else
	{
		*ppv = NULL;
		return E_NOINTERFACE;
	}
	AddRef();
	return S_OK;
}

HRESULT CInsideCOM::Sum(int x, int y, int *retval)
{
	*retval = x + y;
	return S_OK;
}

HRESULT CInsideCOM::Sum2(myDataType test, int *retval)
{
	*retval = test.x + test.y;
	return S_OK;
}

class CFactory : public IClassFactory
{
public:
	// IUnknown
	ULONG __stdcall AddRef();
	ULONG __stdcall Release();
	HRESULT __stdcall QueryInterface(REFIID iid, void** ppv);

	// IClassFactory
	HRESULT __stdcall CreateInstance(IUnknown *pUnknownOuter, REFIID iid, void** ppv);
	HRESULT __stdcall LockServer(BOOL bLock);

	CFactory() : m_cRef(1) { }
	~CFactory() { cout << "Component: CFactory::~CFactory()" << endl; }

private:
	long m_cRef;
};

ULONG CFactory::AddRef()
{
	cout << "Component: CFactory::AddRef() m_cRef = " << m_cRef + 1 << endl;
	return InterlockedIncrement(&m_cRef);
}

ULONG CFactory::Release()
{
	cout << "Component: CFactory::Release() m_cRef = " << m_cRef - 1 << endl;
	unsigned cRef = InterlockedDecrement(&m_cRef);
	if(cRef != 0)
		return cRef;
	delete this;
	return 0;
}

HRESULT CFactory::QueryInterface(REFIID iid, void** ppv)
{
	if((iid == IID_IUnknown) || (iid == IID_IClassFactory))
	{
		cout << "Component: CFactory::QueryInteface() for IUnknown or IClassFactory " << this << endl;
		*ppv = (IClassFactory *)this;
	}
	else 
	{
		*ppv = NULL;
		return E_NOINTERFACE;
	}
	AddRef();
	return S_OK;
}

HRESULT CFactory::CreateInstance(IUnknown *pUnknownOuter, REFIID iid, void **ppv)
{
	if(pUnknownOuter != NULL)
		return CLASS_E_NOAGGREGATION;

	CInsideCOM *pInsideCOM = new CInsideCOM;
	cout << "Component: CFactory::CreateInstance() " << pInsideCOM << endl;

	if(pInsideCOM == NULL)
		return E_OUTOFMEMORY;

	HRESULT hr = pInsideCOM->QueryInterface(iid, ppv);
	pInsideCOM->Release();
	return hr;
}

HRESULT CFactory::LockServer(BOOL bLock)
{
	if(bLock)
		CoAddRefServerProcess();
	else
		if(CoReleaseServerProcess() == 0)
			SetEvent(g_hEvent);
	return S_OK;
}

void RegisterComponent()
{
	ITypeLib* pTypeLib = 0;
	HRESULT hr = LoadTypeLibEx(L"component.exe", REGKIND_DEFAULT, &pTypeLib);
	if(FAILED(hr))
		cout << "LoadTypeLibEx failed" << endl;

	ITypeInfo* pTypeInfo = 0;
	const GUID GUID_myDataType = {0x10000099,0x0000,0x0000,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01};
	hr = pTypeLib->GetTypeInfoOfGuid(GUID_myDataType, &pTypeInfo);
	if(FAILED(hr))
		cout << "GetTypeInfoOfGuid failed" << endl;
	pTypeLib->Release();

	IRecordInfo* pRecordInfo = 0;
	hr = GetRecordInfoFromTypeInfo(pTypeInfo, &pRecordInfo);
	if(FAILED(hr))
		cout << "GetRecordInfoFromTypeInfo failed" << endl;
	pTypeInfo->Release();

	BSTR string = 0;
	hr = pRecordInfo->GetName(&string);

	ULONG size;
	hr = pRecordInfo->GetSize(&size);
	if(FAILED(hr))
		cout << "GetName failed" << endl;
	wprintf(L"name is %s, size is %d bytes\n", string, size);

	Sleep(2000);

	pRecordInfo->Release();
	SysFreeString(string);

	RegisterServer("component.exe", CLSID_InsideCOM, "Inside COM Sample", "Component.InsideCOM", "Component.InsideCOM.1", NULL);
}

void CommandLineParameters(int argc, char** argv)
{
	RegisterComponent();
	if(argc < 2)
	{
		cout << "No parameter, but registered anyway..." << endl;
		exit(false);
	}
	char* szToken = strtok(argv[1], "-/"); 
	if(_stricmp(szToken, "RegServer") == 0)
	{
		RegisterComponent();
		cout << "RegServer" << endl;
		exit(true);
	}
	if(_stricmp(szToken, "UnregServer") == 0)
	{
		UnRegisterTypeLib(LIBID_Component, 1, 0, LANG_NEUTRAL, SYS_WIN32);
		UnregisterServer(CLSID_InsideCOM, "Component.InsideCOM", "Component.InsideCOM.1");
		cout << "UnregServer" << endl;
		exit(true);
	}
	if(_stricmp(szToken, "Embedding") != 0)
	{
		cout << "Invalid parameter" << endl;
		exit(false);
	}
}

void main(int argc, char** argv)
{
	CommandLineParameters(argc, argv);

	CoInitializeEx(NULL, COINIT_MULTITHREADED);
	g_hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);

	DWORD dwRegister;
	IClassFactory *pIFactory = new CFactory();
	CoRegisterClassObject(CLSID_InsideCOM, pIFactory, CLSCTX_LOCAL_SERVER, REGCLS_SUSPENDED|REGCLS_MULTIPLEUSE, &dwRegister);
	CoResumeClassObjects();

	WaitForSingleObject(g_hEvent, INFINITE);
	CloseHandle(g_hEvent);

	CoRevokeClassObject(dwRegister);
	pIFactory->Release();
	CoUninitialize();
}